package cn.bon.emp.service.impl;

import cn.bon.common.exception.BizIllegalException;
import cn.bon.common.pojo.po.Files;
import cn.bon.emp.api.pojo.dto.DetailDTO;
import cn.bon.emp.api.pojo.po.*;
import cn.bon.emp.api.pojo.vo.DetailVO;
import cn.bon.emp.mapper.EmpDetailsMapper;
import cn.bon.emp.service.*;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 员工详情 服务实现类
 * </p>
 *
 * @author luochao
 * @since 2023-12-09
 */
@Service
public class EmpDetailsServiceImpl extends ServiceImpl<EmpDetailsMapper, EmpDetails> implements IEmpDetailsService {

    @Resource
    IEmpInfoService empInfoService;
    @Resource
    IEmpOrgService empOrgService;
    @Resource
    IOrgService orgService;
    @Resource
    IFilesService filesService;
    @Resource
    IEmpJobService empJobService;
    @Resource
    IUniEmpInfoSkillService uniEmpInfoSkillService;

    @Override
    public DetailVO getDetails(Long id) {
        DetailVO vo = new DetailVO();

        //查询details
        EmpDetails empDetails = this.getById(id);
        if (empDetails == null) throw new BizIllegalException("用户不存在！");
        BeanUtils.copyProperties(empDetails, vo);

        //查询基本信息
        EmpInfo empInfo = empInfoService.getById(id);
        if (empInfo == null) throw new BizIllegalException("用户不存在！");
        BeanUtils.copyProperties(empInfo, vo);

        //查询岗位
        vo.setJobName(empJobService.getById(empInfo.getEmpJobId()).getEmpJobName());

        //查询部门信息
        EmpOrg empOrg = empOrgService.getById(id);
        if (empOrg != null) {
            Org comp = orgService.getById(empOrg.getCompanyId());
            Org dept = orgService.getById(empOrg.getDeptId());
            Org section = orgService.getById(empOrg.getSectionId());
            Org group = orgService.getById(empOrg.getGroupId());
            vo.setCompany(comp == null ? "" : comp.getOrgName());
            vo.setDept(dept == null ? "" : dept.getOrgName());
            vo.setSection(section == null ? "" : section.getOrgName());
            vo.setGroup(group == null ? "" : group.getOrgName());
        }

        //资质技能
        LambdaQueryWrapper<UniEmpInfoSkill> skillLambdaQueryWrapper = new LambdaQueryWrapper<>();
        skillLambdaQueryWrapper.eq(UniEmpInfoSkill::getEmpId, id);
        List<UniEmpInfoSkill> skills = uniEmpInfoSkillService.list(skillLambdaQueryWrapper);
        vo.setSkills(skills);

        //查询文件信息
        LambdaQueryWrapper<Files> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Files::getComId, id);
        List<Files> files = filesService.list(queryWrapper);
        vo.setFiles(files);

        return vo;
    }

    @Override
    public void updateEmp(Long id, DetailDTO dto) {
        //1.更新skill
        //1.1删除原先skill
        LambdaQueryWrapper<UniEmpInfoSkill> skillLambdaQueryWrapper = new LambdaQueryWrapper<>();
        skillLambdaQueryWrapper.eq(UniEmpInfoSkill::getEmpId, id);
        uniEmpInfoSkillService.remove(skillLambdaQueryWrapper);
        //1.2更新新skill
        List<UniEmpInfoSkill> skills = dto.getSkills();
        for (UniEmpInfoSkill skill : skills) {
            skill.setEmpId(id);
            uniEmpInfoSkillService.updateById(skill);
        }

        //更新details
        dto.setEmpId(id);
        this.updateById(dto);
    }
}
